﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Xml.Linq;

namespace TD.TheGame
{
    public abstract class SpriteB : DrawableGameComponent
    {
        public bool Active
        {
            get;
            set;
        }

        public bool isCollide
        {
            get;
            set;
        }

        public SpriteEnumState State
        {
            get;
            set;
        }

        public Texture2D Texture
        {
            get;
            private set;
        }

        public Vector2 Position;

        public Color Color
        {
            get;
            set;
        }

        public Rectangle BoundingRect
        {
            get;
            set;
        }

        protected SpriteBatch _batch;
        protected Rectangle[] _rects;
        public int _rectCurrent;

        //the current max amount of sprite to update to
        protected int max;

        private string filePath;

        protected KeyBoardInput keyInput;

        public SpriteB(Texture2D texture,Vector2 position, bool active, string fileName, Game game) : base(game)
        {
            keyInput = (KeyBoardInput)Game.Services.GetService(typeof(KeyBoardInput));

            this.filePath = (Environment.CurrentDirectory + "\\XML\\" + fileName);
            Texture = texture;
            Position = position;
            Active = active;
            _batch = new SpriteBatch(Game.GraphicsDevice);
            if (fileName != "" && System.IO.File.Exists(this.filePath))
            {
                //init the proper Rects;
                XElement xelement = XElement.Load(filePath);
                // Read the entire XML
                int count = xelement.Descendants("frame").Count();
                _rects = new Rectangle[count];
                var frames = from frame in xelement.Descendants("frame")
                             select new
                             {
                                 X = frame.Element("layers").Element("layer").Element("rect").Attribute("x"),
                                 Y = frame.Element("layers").Element("layer").Element("rect").Attribute("y"),
                                 W = frame.Element("layers").Element("layer").Element("rect").Attribute("width"),
                                 H = frame.Element("layers").Element("layer").Element("rect").Attribute("height")
                             };
                int rectCount = 0;
                foreach (var rect in frames)
                {
                    _rects[rectCount] = new Rectangle((int)rect.X, (int)rect.Y, (int)rect.W, (int)rect.H);
                    rectCount++;
                }

                BoundingRect = new Rectangle((int)position.X, (int)position.Y, _rects[_rectCurrent].Width, _rects[_rectCurrent].Height);
                    //_rects[_rectCurrent];
            }
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public override void Initialize()
        {
            if (filePath == "")
            {
                BoundingRect = _rects[_rectCurrent];

            }
            base.Initialize();
        }

        abstract public override void Update(GameTime gameTime);

        /// <summary>
        /// Draw
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            if (Active == true)
            {
                _batch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied);
                _batch.Draw(Texture, Position, _rects[_rectCurrent], Color.White);   
                _batch.End();
            }
            base.Draw(gameTime);
        }

        public void initBoundingRect()
        {
            BoundingRect = new Rectangle((int)Position.X, (int)Position.Y, _rects[_rectCurrent].Width, _rects[_rectCurrent].Height);
        }

    }
}